package com.zhanglu.fun.services.chatroom.robot;

import com.zhanglu.fun.services.chatroom.bean.ResultResponse;
import com.zhanglu.fun.services.chatroom.client.RobotClient;
import com.zhanglu.fun.services.chatroom.robot.bean.enums.QuestionType;
import com.zhanglu.fun.services.chatroom.robot.bean.response.CaishenyeResponse;
import com.zhanglu.fun.services.chatroom.robot.bean.response.GuanyinsiResponse;
import com.zhanglu.fun.services.chatroom.robot.bean.response.JokeResponse;
import com.zhanglu.fun.services.chatroom.robot.bean.response.YuelaoResponse;
import com.zhanglu.fun.tookit.json.JsonTools;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @author zhanglu
 * @description 聊天机器人业务处理
 * @date 2017/10/30 19:22
 */
@Service
@Slf4j
public class RobotService {
    @Autowired
    private RobotClient robotClient;

    public ResultResponse getAnswer(String question) {
        if (StringUtils.isBlank(question)) {
            return null;
        }
        log.debug("param is {}", question);
        if (question.contains(QuestionType.笑话.toString())) {
            return new ResultResponse<>(true, "joke", getJoke(question));
        }

        if (Objects.equals(question, QuestionType.观音灵签.toString())) {
            return new ResultResponse<>(true, "guanyin", getGuanyinLQ(question));
        }

        if (Objects.equals(question, QuestionType.月老灵签.toString())) {
            return new ResultResponse<>(true, "yuelao", getYuelaoLQ(question));
        }

        if (Objects.equals(question, QuestionType.财神爷灵签.toString())) {
            return new ResultResponse<>(true, "caishenye", getCaishenyeLQ(question));
        }

        return new ResultResponse<>(true, "text", robotClient.getRobotAnswer(question));
    }

    public JokeResponse getJoke(String question) {
        String answer = robotClient.getRobotAnswer(question);
        log.debug("answer:{}", answer);
        JokeResponse response = JsonTools.parseObject(answer, JokeResponse.class);
        log.debug("answer format:{}", JsonTools.showJSONStringFormat(response));
        return response;
    }

    public GuanyinsiResponse getGuanyinLQ(String question) { // 观音灵签，下面也是这样
        String answer = robotClient.getRobotAnswer(question);
        log.debug("answer:{}", answer);
        GuanyinsiResponse response = JsonTools.parseObject(answer, GuanyinsiResponse.class);
        log.debug("answer format:{}", JsonTools.showJSONStringFormat(response));
        return response;
    }

    public YuelaoResponse getYuelaoLQ(String question) {
        String answer = robotClient.getRobotAnswer(question);
        log.debug("answer:{}", answer);
        YuelaoResponse response = JsonTools.parseObject(answer, YuelaoResponse.class);
        log.debug("answer format:{}", JsonTools.showJSONStringFormat(response));
        return response;
    }

    public CaishenyeResponse getCaishenyeLQ(String question) {
        String answer = robotClient.getRobotAnswer(question);
        log.debug("answer:{}", answer);
        CaishenyeResponse response = JsonTools.parseObject(answer, CaishenyeResponse.class);
        log.debug("answer format:{}", JsonTools.showJSONStringFormat(response));
        return response;
    }
}
